﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ChessBoard : NetworkBehaviour {

	public static ChessBoard _instance;

	public int[, ] grid;
	public float timer = 0;
	public bool gameStart;

	public ChessType turn;
	public Transform parents;
	public GameObject undoButton;
	public GameObject[] prefabs;
	public Stack<Transform> chessTack = new Stack<Transform> ();

	List<Player> players = new List<Player> ();

	public static ChessBoard Instance {
		get {
			return _instance;
		}
	}

	public bool PlayChess (int[] pos) {
		if (!gameStart) {
			return false;
		}
		if (pos[0] < 0 || pos[0] > 15 ||
			pos[1] < 0 || pos[1] > 15) {
			return false;
		}
		if (grid[pos[0], pos[1]] != 0) {
			return false;
		}
		GameObject chessChild;
		switch (turn) {
			case ChessType.Black:
				chessChild = chessChild = Instantiate (prefabs[0], new Vector3 (pos[0] - 7, pos[1] - 7), Quaternion.identity);
				grid[pos[0], pos[1]] = 1;
				turn = ChessType.White;
				break;
			case ChessType.White:
				chessChild = chessChild = Instantiate (prefabs[1], new Vector3 (pos[0] - 7, pos[1] - 7), Quaternion.identity);
				grid[pos[0], pos[1]] = 2;
				turn = ChessType.Black;
				break;
			default:
				chessChild = null;
				break;
		}
		if (CheckWinner (pos)) {
			GameOver ();
		}
		chessTack.Push (chessChild.transform);
		chessChild.transform.SetParent (parents);
		NetworkServer.Spawn (chessChild);
		CheckUndoButton ();
		return true;
	}

	public void ReChess () {
		if (chessTack.Count > 0 && gameStart) {
			Transform pos = chessTack.Pop ();
			if (grid[(int) (pos.position.x + 7), (int) (pos.position.y + 7)] == 1) {
				turn = ChessType.Black;
			}
			if (grid[(int) (pos.position.x + 7), (int) (pos.position.y + 7)] == 2) {
				turn = ChessType.White;
			}
			grid[(int) (pos.position.x + 7), (int) (pos.position.y + 7)] = 0;
			Destroy (pos.gameObject);
		} else if (!gameStart) {
			Button undo = undoButton.GetComponent<Button> ();
			undo.image.sprite = Resources.Load<Sprite> ("bt_regret_0");
		}
	}

	public void AddPlayer (Player player) {
		if (!players.Contains (player)) {
			switch (players.Count) {
				case 0:
					player.chessColor = ChessType.Black;
					break;
				case 1:
					player.chessColor = ChessType.White;
					gameStart = true;
					break;
				default:
					player.chessColor = ChessType.Watch;
					break;
			}
			players.Add (player);
		}
	}

	public void DelPlayer (Player player) {
		if (players.Contains (player)) {
			players.Remove (player);
		}
	}

	private void Start () {
		if (Instance == null) {
			_instance = this;
		}
		gameStart = false;
		turn = ChessType.Black;
		grid = new int[15, 15];
		chessTack = new Stack<Transform> ();
		CheckUndoButton ();
	}

	private void FixedUpdate () {
		timer += Time.deltaTime;
	}

	private void CheckUndoButton () {
		Button undo = undoButton.GetComponent<Button> ();
		if (chessTack.Count <= 1) {
			undo.image.sprite = Resources.Load<Sprite> ("bt_regret_2");
		} else {
			undo.image.sprite = Resources.Load<Sprite> ("bt_regret_0");
		}
	}

	private bool CheckWinner (int[] pos) {
		if (CheckOneLine (pos, new int[2] { 1, 0 })) { return true; } //横轴扫描棋子
		if (CheckOneLine (pos, new int[2] { 0, 1 })) { return true; } //纵轴扫描棋子
		if (CheckOneLine (pos, new int[2] { 1, 1 })) { return true; } //斜轴扫描棋子
		if (CheckOneLine (pos, new int[2] { 1, -1 })) { return true; } //斜轴扫描棋子
		return false;
	}

	// pos 棋子位置   offset 偏移量
	private bool CheckOneLine (int[] pos, int[] offset) {
		int linkNum = 1; // 计同一色的棋子个数
		// 从左往右扫描
		for (int i = offset[0], j = offset[1];
			(pos[0] + i >= 0) && (pos[0] + i < 15) && (pos[1] + j >= 0) && (pos[1] + j < 15); i += offset[0], j += offset[1]) {
			// 判断路径上的棋子是否和第一个棋子颜色相同
			if (grid[pos[0] + i, pos[1] + j] == (int) turn) {
				linkNum++;
			} else {
				break;
			}
		}
		// 从右往左扫描
		for (int i = -offset[0], j = -offset[1];
			(pos[0] + i >= 0) && (pos[0] + i < 15) && (pos[1] + j >= 0) && (pos[1] + j < 15); i -= offset[0], j -= offset[1]) {
			// 判断路径上的棋子是否和第一个棋子颜色相同
			if (grid[pos[0] + i, pos[1] + j] == (int) turn) {
				linkNum++;
			} else {
				break;
			}
		}
		if (linkNum >= 5) {
			return true;
		}
		return false;
	}

	//游戏结束
	private void GameOver () {
		switch (turn) {
			case ChessType.Watch:
				break;
			case ChessType.Black:
				Debug.Log ("白棋胜利！！！！");
				break;
			case ChessType.White:
				Debug.Log ("黑棋胜利！！！！");
				break;
		}
		gameStart = false;
	}
}